package com.ygqh.baby.service.impl;

import java.util.Date;
import java.util.List;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgAlipayRefundRecord;
import com.ygqh.baby.mapper.YgAlipayRefundRecordMapper;
import com.ygqh.baby.service.YgAlipayRefundRecordService;

import com.ygqh.baby.service.alipay.model.SuccessCallBack;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.YgAlipayRefundRecordExample;
import com.ygqh.baby.ao.DataStatus;

@Service
public class YgAlipayRefundRecordServiceImpl implements YgAlipayRefundRecordService {

	private final ExecutorService executorService = Executors.newFixedThreadPool(3);

	@Autowired
	private YgAlipayRefundRecordMapper ygAlipayRefundRecordMapper;

	@Override
	public List<YgAlipayRefundRecord> find() {
		YgAlipayRefundRecordExample example = new YgAlipayRefundRecordExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygAlipayRefundRecordMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgAlipayRefundRecordExample example = new YgAlipayRefundRecordExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygAlipayRefundRecordMapper.countByExample(example);
	}

	@Override
	public YgAlipayRefundRecord findById(Long id) {
		return ygAlipayRefundRecordMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgAlipayRefundRecord ygAlipayRefundRecord) {
		return ygAlipayRefundRecordMapper.insertSelective(ygAlipayRefundRecord);
	}

	@Override
	public int update(YgAlipayRefundRecord ygAlipayRefundRecord) {
		return ygAlipayRefundRecordMapper.updateByPrimaryKeySelective(ygAlipayRefundRecord);
	}

	@Override
	public ResultSet<YgAlipayRefundRecord> search(QueryInfo queryInfo, String q) {
		List<YgAlipayRefundRecord> list = ygAlipayRefundRecordMapper.selectSuper(queryInfo, q);
		int count = ygAlipayRefundRecordMapper.countSuper(q);
		ResultSet<YgAlipayRefundRecord> resultSet = new ResultSet<YgAlipayRefundRecord>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgAlipayRefundRecordExample example = new YgAlipayRefundRecordExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgAlipayRefundRecord record = new YgAlipayRefundRecord();
		record.setStatus(DataStatus.Delete);

		return ygAlipayRefundRecordMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgAlipayRefundRecord ygAlipayRefundRecord) {
		int r = 0;
		if (ygAlipayRefundRecord.getId() != null) {
			r = this.update(ygAlipayRefundRecord);
		} else {
			r = this.save(ygAlipayRefundRecord);
		}
		return r;
	}

	@Override
	public YgAlipayRefundRecord findByBatchNo(String batchNo) {
		YgAlipayRefundRecordExample example = new YgAlipayRefundRecordExample();
		example.createCriteria().andBatchNoEqualTo(batchNo).andStatusEqualTo(DataStatus.Valid.name());
		List<YgAlipayRefundRecord> list = ygAlipayRefundRecordMapper.selectByExample(example);
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public int updateSuccess(String batchNo, Date successTime, String resultDetails) {

		return ygAlipayRefundRecordMapper.updateSuccess(batchNo, successTime, resultDetails);
	}

	@Override
	public void refundSuccess(String batchNo, String resultDetails, SuccessCallBack successCallBack) {
		executorService.execute(() -> {
			this.updateSuccess(batchNo, new Date(), resultDetails);
			YgAlipayRefundRecord refundRecord = this.findByBatchNo(batchNo);
			successCallBack.execute(refundRecord.getReturnId(), refundRecord.getCreateBy());
		});
	}

}
